//
//  SliderView.swift
//  手势
//
//  Created by 王苏进 on 18/5/17.
//  Copyright © 2017年 王苏进. All rights reserved.
//

import UIKit

class SliderView: UIView {

    let sliderView = UIView()
    
    lazy var tipLabel: UILabel = {
    
          let l = UILabel()
          l.textColor = UIColor.white
          l.backgroundColor = UIColor.purple
          l.layer.cornerRadius = 10
          l.layer.masksToBounds = true
          return l
        
    }()
    
    var currentPointX: CGFloat = 0.0
    
    lazy var shapeLayer: CAShapeLayer = {
        
        let s = CAShapeLayer()
        s.fillColor = UIColor.orange.cgColor
        
        return s
    }()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setUp()
    }
    
//    override init(frame: CGRect) {
//        super.init(frame: frame)
//        setUp()
//    }
//    
//   
//    
//    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
    func setUp() {
        
        
        tipLabel.text = "0%"
        sliderView.backgroundColor = UIColor.gray
        
        
        addSubview(tipLabel)
        addSubview(sliderView)
        
        sliderView.layer.addSublayer(shapeLayer)
        
        let pan = UIPanGestureRecognizer(target: self, action: #selector(panAction(pan:)))
        
        sliderView.addGestureRecognizer(pan)
        
    }
    
    func panAction(pan: UIPanGestureRecognizer)  {
        
        let w = bounds.size.width - 40
        
        let x  = pan.location(in: sliderView).x
        
        
        if x>=0 && x<=(w ) {
            currentPointX =  pan.location(in: sliderView).x
            tipLabel.text = "\(Float(currentPointX/w) * 100)%"
            tipLabel.sizeToFit()
            
            setNeedsDisplay()
            
            
            layoutIfNeeded()
        }
        
        if pan.state == UIGestureRecognizerState.began {
            UIView.animate(withDuration: 0.3, animations: {
                self.tipLabel.transform = CGAffineTransform(translationX: self.tipLabel.transform.tx, y:100)
                self.tipLabel.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
                self.tipLabel.textColor = UIColor.orange
                
            }, completion: nil)
        }
        if pan.state == UIGestureRecognizerState.ended {
            UIView.animate(withDuration: 0.3, animations: {
                self.tipLabel.transform = CGAffineTransform.identity
                self.tipLabel.textColor = UIColor.white
            }, completion: nil)
        }
        
        
       
       
        
    }
    

    
    override func draw(_ rect: CGRect) {
        
        let frame = sliderView.frame
        
       
        let path = UIBezierPath(rect: CGRect(x: 0 , y: 0, width: currentPointX, height: frame.size.height))
        
       
        path.move(to: CGPoint(x: currentPointX, y: 0))
        shapeLayer.path = path.cgPath
        UIColor.red.set()
        
        
        
        
        
        
        
        
    }
    
    override func layoutSubviews() {
        
        let w = bounds.size.width - 40
        
        tipLabel.frame = CGRect(x: 20, y: 40, width:100, height: 30)
        sliderView.frame = CGRect(x: 20, y: 80, width: w, height: 64)
        
       
        
        super.layoutSubviews()
        
    }
    
   
    

}
